﻿@model IEnumerable<PokerLife.Models.SYS.UserViewModel>

@{
    ViewBag.Title = "User";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}

@(Html.Kendo().Grid(Model)
        .Name("Grid")
        .Columns(columns =>
    {
        if (FlexBMS.Class.FormatGrid.User(columns))
        {
            columns.Command(command =>
            {
                command.Custom("Edit").Click("Edit");
                command.Custom("Delete").Click("DeleteRow");

            }).Width(170);
        }
    })
        .ToolBar(toolbar =>
                    {
                        toolbar.Template(@<text>
<div style="float: left">
    <div class="toolbar">
        <a class="k-button toolbarbuttonsize" id="btnAdd"><span></span>Add</a>
        <a class='k-button toolbarbuttonsize' id='DeleteMultiRows'><span></span>Delete</a>
    </div>
</div>

<div style="float: right;">
    <a class="k-button toolbarbuttonsize" id="btnFormat"><span></span>Format</a>
</div>
</text>);
                    }
                )
        .Pageable
        (pager => pager
            .Input(true)
            .Numeric(true)
            .Info(true)
            .PreviousNext(true)
            .Refresh(true)
            .PageSizes(new int[] { 10, 20, 50 }))
        .Sortable()
        .Scrollable()
        .Resizable(resize => resize.Columns(true))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("User_Load", "User"))
        )
    )

@(Html.Kendo().Window()
    .Name("window")
    .Actions(actions => actions
        .Minimize()
        .Maximize()
        .Close()
    )
    .Title("User")
    .Visible(false)
    .Iframe(true)
    .Modal(true)
    .Width(470)
    .Height(220)
    .Draggable()
    .Resizable()
    .Events(events => events.Close("CloseWindow"))
)

@(Html.Kendo().Window()
    .Name("windowformat")
    .Actions(actions => actions
        .Minimize()
        .Maximize()
        .Close()
    )
    .Title("Cấu hình hiển thị")
    .Visible(false)
    .Iframe(true)
    .Modal(true)
    .Width(980)
    .Height(450)
    .Draggable()
    .Resizable()
    .Events(events => events.Close("CloseWindow"))
)

<style>

</style>

<script>

    $(document).ready(function () {
        resizeGrid();
        $('#ckSelectAll').prop('checked', false);
        $('#DeleteMultiRows').hide();
    });

    var count_check = 0;
    $('#ckSelectAll').click(function (e) {
        if ($(this).is(':checked')) {
            $('#Grid .k-grid-content').find('.chkbx').prop('checked', true);
            $('#DeleteMultiRows').show();
            count_check = $("#Grid").data("kendoGrid").dataSource.view().length;
        }
        else {
            $('#Grid .k-grid-content').find('.chkbx').prop('checked', false);
            $('#DeleteMultiRows').hide();
            count_check = 0;
        }
    });
    
    //Checkbox hiện và ẩn Button Xóa
    $("#Grid").delegate(".chkbx", "click", function (e) {
        if ($(this).is(':checked')) {
            count_check++;
            $('#DeleteMultiRows').show();
            if (count_check == $("#Grid").data("kendoGrid").dataSource.view().length) { $('#ckSelectAll').prop("checked", true); }
        }
        else {
            count_check--;
            $('#ckSelectAll').prop("checked", false);
            if (count_check == 0) {
                $('#DeleteMultiRows').hide();
            }
            else {
                $('#DeleteMultiRows').show();
            }
        }
    });

    function CloseWindow(e) {
        //location.reload();
    }

    $('#btnAdd').click(function (e) {
        e.preventDefault();
        var dialog = $("#window").data("kendoWindow");

        dialog.refresh({
            url: '@Url.Content("~/User/AddEdit")'
        });

        dialog.center().open();
    });

function Edit(e) {
    e.preventDefault();
    var dialog = $("#window").data("kendoWindow");
    var item = this.dataItem($(e.currentTarget).closest("tr"));

    dialog.refresh({
        url: '@Url.Content("~/User/AddEdit/?intIDSys=")' + item.IDSys
        });
        dialog.center().open();
    };

    function DeleteRow(e) {
        e.preventDefault();
        var item = this.dataItem($(e.currentTarget).closest("tr"));

        $.when(kendo.ui.ExtYesNoDialog.show({ title: 'Warning', message: '<b>Are you sure detele?</b>', icon: 'k-ext-warning', width: '400px' }))
           .done(function (response) {
               if (response.button == 'Yes') {
                   $.ajax({
                       url: '@Url.Content("~/User/Delete")',
                       type: 'POST',
                       dataType: 'json',
                       data: { intIDSys: item.IDSys },
                       traditional: true,
                       success: function (result) {
                           kendo.ui.ExtAlertDialog.show({ title: "Thông báo!", message: result.Message });
                           var grid = $("#Grid").data("kendoGrid");
                           var $tr = $(e.currentTarget).closest("tr");
                           grid.removeRow($tr);
                       },
                       error: function (event, request, settings) {
                           alert(settings);
                       }
                   });
    }
           });
};

    $('#DeleteMultiRows').click(function (e) {

        $.when(kendo.ui.ExtYesNoDialog.show({ title: 'Warning', message: '<b>Are you sure detele?</b>', icon: 'k-ext-warning', width: '400px' }))
           .done(function (response) {
               if (response.button == 'Yes') {
                   var arrID = new Array();
                   $('#Grid .k-grid-content .chkbx').each(function () {
                       if ($(this).is(':checked')) {
                           var grid = $('#Grid').data().kendoGrid;
                           var dataItem = grid.dataItem($(this).closest('tr'));
                           arrID.push(dataItem.IDSys);
                       }
                   });

                   $.ajax({
                       url: '@Url.Content("~/User/DeleteList")',
                       type: 'POST',
                       dataType: 'json',
                       data: { lstIDSys: arrID },
                       traditional: true,
                       success: function (result) {
                           kendo.ui.ExtAlertDialog.show({ title: "Thông báo!", message: result.Message });
                           var grid = $("#Grid").data("kendoGrid");
                           grid.dataSource.read();
                       },
                       error: function (event, request, settings) {
                           alert(settings);
                       }
                   });
               }
           });
    });

        $('#Grid tbody').on('click', 'a.Click_Popup', function (e) {
            e.preventDefault();
            var dialog = $("#window").data("kendoWindow");
            $tr = $(this).parent().parent();
            var item = $("#Grid").data("kendoGrid").dataItem($tr);
            dialog.refresh({
                url: '@Url.Content("~/User/AddEdit/?intIDSys=")' + item.IDSys
        });
        dialog.center().open();
    })

        $('#btnFormat').click(function (e) {
            e.preventDefault();
            var dialog = $("#windowformat").data("kendoWindow");
            var col = ($('#Grid').data('kendoGrid')).columns;
            var ColumnList = "";

            for (var i = 0 ; i < col.length; i++) {
                if (col[i].field != null && col[i].field.length > 0)
                    ColumnList += col[i].field + ",";
            }

            dialog.refresh({
                url: '@Url.Content("~/DisplayConfig/")' + '?TableName=SYS_User&ColumnList=' + ColumnList
        });

        dialog.center().open();
    });


</script>
